package com.ssish.common.insurance.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FtpUtil {
	/**
	 * 
	 *新建路径
	 * @throws Exception 
	 */
	public static Boolean makeDirectory(String hostname,String username,String password,String path) throws Exception{
		Boolean flag = Boolean.TRUE;
		FTPClient ftp = new FTPClient();
		ftp.setDefaultTimeout(10000);
		try{
			ftp.connect(hostname);
			if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
				throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
            	throw new Exception("FTP用户名密码错误");
            }
            ftp.makeDirectory(path);
            ftp.logout();
		}catch(Exception e){
			flag = Boolean.FALSE;
			e.printStackTrace();
			throw e;
		}finally{
			try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
            	e.printStackTrace();
            }
		}
		return flag;
	}
	
	
	/**
	 * 
	 *上传文件
	 * @throws Exception 
	 */
	public static Boolean putFile(String hostname,String username,String password,String remoteFilename,InputStream is) throws Exception {
		Boolean flag = Boolean.TRUE;
		FTPClient ftp = new FTPClient();
		ftp.setDefaultTimeout(10000);
//		ftp.setDataTimeout(100000);
		try{
			ftp.connect(hostname,21);
			if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
				throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
            	throw new Exception("FTP用户名密码错误");
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            if (!ftp.storeFile(remoteFilename, is)) {
            	throw new Exception(ftp.getReplyString()+ftp.getReply());
            }
            ftp.logout();
		}catch(Exception e){
			flag = Boolean.FALSE;
			e.printStackTrace();
			throw e;
		}finally{
			try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
            }
            try {
                is.close();
            } catch (Exception e) {
            }
		}
		return flag;
	}
	
	/**
	 * 
	 *下载文件
	 */
	public static byte[] getFile(String hostname,String username,String password,String remoteFilename,OutputStream out) throws Exception {
		FTPClient ftp = new FTPClient();
		ftp.setDefaultTimeout(10000);
		byte[] return_buf = null;
		try{
			ftp.connect(hostname);
			if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
				throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
            	throw new Exception("FTP用户名密码错误");
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            InputStream in = ftp.retrieveFileStream(remoteFilename);
            ByteArrayOutputStream byteout = new ByteArrayOutputStream();
            byte[] buf = new byte[204800];
            int bufsize = 0;
            while ((bufsize =in.read(buf,0,buf.length)) != -1) {
            	byteout.write(buf,0,bufsize);
				
			}
            return_buf = byteout.toByteArray();
            byteout.close();
            in.close();
//            if (!ftp.retrieveFile(remoteFilename, out)) {
//            	throw new Exception(ftp.getReplyString()+ftp.getReply());
//            }
            ftp.logout();
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
            }
		}
		return return_buf;
	}
	
	//删除文件
	public static String delFile(String hostname,String username,String password,String remoteFilename) throws Exception {
		FTPClient ftp = new FTPClient();
		ftp.setDefaultTimeout(10000);
		try{
			ftp.connect(hostname);
			if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
				throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
            	throw new Exception("FTP用户名密码错误");
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            //ftp.enterLocalPassiveMode();
            ftp.deleteFile(remoteFilename);
            ftp.logout();
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
            }
		}
		return null;
	}
}
